﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Sample.Models;
using MvcApi;

namespace Sample.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        public static List<Person> _people;

        private static void LoadPeople()
        {
            _people = new List<Person>() {
                new Person() { FirstName = "Nick", LastName = "Riggs", Age = 30 },
                new Person() { FirstName = "John", LastName = "Smith", Age = 45 }
            };
        }

        static HomeController() { LoadPeople(); }

        public HomeController()
        {
            // just some demo-server protection
            if (_people.Count > 100)
                LoadPeople();
        }

        public ActionResult Index()
        {
            var model = _people.ToArray();

            return View(model);
        }

        [Api]
        public ActionResult Create(Person person)
        {
            person.Id = Guid.NewGuid();
            _people.Add(person);

            return View(person);
        }
    }
}
